/**
 * 什么是临界区(二)
 *
 * @author fireway
 * @since 2019年 01月 08日 星期二 07:18:13 CST
 */
package concurrency;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class ExplicitPairManager1 extends PairManager {
    private Lock mLock = new ReentrantLock();

    @Override
    public void increase() {
        mLock.lock();
        try {
            mPair.increaseX();
            mPair.increaseY();
            store(getPair());
        } finally {
            mLock.unlock();
        }
    }
}

class ExplicitPairManager2 extends PairManager {
    private Lock mLock = new ReentrantLock();

    @Override
    public void increase() {
        Pair temp = null;
        mLock.lock();
        try {
            mPair.increaseX();
            mPair.increaseY();
            temp = getPair();
        } finally {
            mLock.unlock();
        }

        store(temp);
    }
}

public class ExplicitCriticalSection {
    public static void main(String[] args) {
        PairManager manager1 = new ExplicitPairManager1();
        PairManager manager2 = new ExplicitPairManager2();
        CriticalSection.testCompare(manager1, manager2);
    }
}
